package com.bidevalution.support.websocket;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.bidevalution.support.context.ChannelContext;

import io.netty.channel.ChannelHandlerContext;

/**
 * 用户连接通道管理
 * 
 * @author 戴志锋
 *
 */
@Component
public class WebSocketuUserHandler {
	private Map<String, BusinessChannelContainer> onlineUserMap = new ConcurrentHashMap<String, BusinessChannelContainer>();
	private static final Logger LOGGER = Logger.getLogger(WebSocketuUserHandler.class);
//
//	public void addChannel(String businessId, String userId,ChannelContext channelContext) {
//		BusinessChannelContainer businessChannelContainer = onlineUserMap.get(businessId);
//		if (businessChannelContainer == null) {
//			businessChannelContainer = new BusinessChannelContainer(businessId);
//		}
//		List<ChannelContext> userChannelContextList = businessChannelContainer.get(userId);
//		if (userChannelContextList == null) {
//			userChannelContextList = new ArrayList<ChannelContext>();
//		}
//		if (!userChannelContextList.contains(channelContext)) {
//			userChannelContextList.add(channelContext);
//		}
//		//businessChannelContainer.put(userId, userChannelContextList);
//		onlineUserMap.put(businessId, businessChannelContainer);
//	}

//	public List<ChannelContext> getChannel(String businessId, String userId) {
//		BusinessChannelContainer businessChannelContainer = onlineUserMap.get(businessId);
//		if (businessChannelContainer == null) {
//			return null;
//		}
//		return businessChannelContainer.get(userId);
//	}
	public List<ChannelContext> getChannel(String businessId) {
		BusinessChannelContainer businessChannelContainer = onlineUserMap.get(businessId);
		if (businessChannelContainer == null) {
			return null;
		}
		List<ChannelContext> channelList = new ArrayList<ChannelContext>();
//		for(Entry<String, List<ChannelContext>> entry:businessChannelContainer.entrySet()) {
//			channelList.addAll(entry.getValue());
//		}
		return channelList;
	}

	/**
	 * 获取总连接数
	 * 
	 * @return
	 */
	public int getChannelCount() {
		int count = 0;
		for (Entry<String, BusinessChannelContainer> entry : onlineUserMap.entrySet()) {
			count = count + entry.getValue().size();
		}
		return count;
	}

	public void remove(ChannelHandlerContext channelHandlerContext) {
//		label: for (Entry<String, BusinessChannelContainer> entry : onlineUserMap.entrySet()) {
//			for (List<ChannelContext> ctxList : entry.getValue().values()) {
//				for(ChannelContext channelContext:ctxList) {
//					if(channelContext.getChannelHandlerContext().channel().id().asLongText().equals(channelHandlerContext.channel().id().asLongText())) {
//						ctxList.remove(channelContext);
//						LOGGER.info("正在移除握手实例...");
//						//Constant.webSocketHandshakerMap.remove(channelContext.getChannelHandlerContext().channel().id().asLongText());
//						//LOGGER.info(MessageFormat.format("已移除握手实例，当前握手实例总数为：{0}", Constant.webSocketHandshakerMap.size()));
//						LOGGER.info(MessageFormat.format("userId为 {0} 的用户已退出聊天，当前在线人数为：{1}", entry.getKey(),
//								getChannelCount()));
//						break label;
//					}
//				}
//			}
//		}
	}

}
